using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using Cit.CDE.Net.Module;
using System.Data;
using Cit.CDE.Net.Module.Commons;
using cde.UserControl;
using System.Xml;
using System.Text.RegularExpressions;

[assembly: TagPrefix("UI.UserControl", "ui")]
namespace UI.UserControl
{
    public class SimpleResultDataGrid : System.Web.UI.WebControls.DataGrid
    {
        private Unit divHeight;
        public Unit DivHeight
        {
            get
            {
                return divHeight;
            }
            set
            {
                divHeight = value;
            }

        }

        private Unit divWidth;
        public Unit DivWidth
        {
            get
            {
                return divWidth;
            }
            set
            {
                divWidth = value;
            }
        }

        protected int elseWidth = 50;
        public Unit ElseWidth
        {
            get
            {
                return new Unit(50);
            }
            set
            {
                elseWidth = (int)value.Value;
            }
        }

        private Unit dispWidth;
        public Unit DispWidth
        {
            get
            {
                return dispWidth;
            }
            set
            {
                dispWidth = value;
            }
        }

        protected int clmStartIndex = 1;
        public int ClmStartIndex
        {
            get
            {
                if (clmStartIndex < 0) clmStartIndex = 0;
                return clmStartIndex;
            }
            set
            {
                clmStartIndex = value;
            }
        }

        private CDEEntityManager _em;
        public CDEEntityManager EntityManager
        {
            set
            {
                _em = value;
                setColumns();
            }
            get
            {
                return _em;
            }
        }

        private List<CDEEntity> _ListEntity;
        public List<CDEEntity> ListEntity
        {
            set
            {
                _ListEntity = value;
                DataSource = ListEntityToDataTable();
            }
            get
            {
                return _ListEntity;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (0 == dispWidth.Value) dispWidth = new Unit("4");
            base.OnInit(e);
        }

        protected virtual DataTable ListEntityToDataTable()
        {
            StringBuilder strXml = new StringBuilder();
            strXml.AppendLine("<Entities>");

            foreach (CDEEntity entity in _ListEntity)
            {
                strXml.AppendLine(Cit.CDE.Net.Service.ModuleFactory.Module2Xml.Entity2Node(entity).Replace("$NULL$",""));
            }
            strXml.AppendLine("</Entities>");

            DataSet ds = new DataSet();
            StringReader sr = new StringReader(strXml.ToString());
            XmlTextReader xr = new XmlTextReader(sr);
            ds.ReadXml(xr);

            if (ds.Tables["Entity"] != null)
            {
                DataTable dt = ds.Tables["Entity"];

                string strName = string.Empty;

                foreach (DataColumn column in dt.Columns) 
                {
                    if (column.ColumnName.EndsWith("ID_NO") || column.ColumnName.EndsWith("CARD_ID")) 
                    {
                        strName = column.ColumnName;
                        break;
                    }
                }
                if (strName.Length>0) 
                {
                    foreach (DataRow row in dt.Rows) 
                    {
                        if (row[strName].ToString().Length == 15) 
                        {
                            row[strName] = row[strName].ToString().Substring(0, 8) + "****" + row[strName].ToString().Substring(12, 3);
                        }
                        else if (row[strName].ToString().Length == 18) 
                        {
                            row[strName] = row[strName].ToString().Substring(0, 10) + "****" + row[strName].ToString().Substring(14, 4);
                        }
                        
                    }
                }
                return dt;
            }
            else
            {
                return new DataTable("Entity");
            }
        }

        protected virtual void setColumns()
        {
            int sum_width = 0;
            int clmIndex = ClmStartIndex;

            foreach (CDEAttribute attribute in _em.ListAttributes)
            {
                if (CDEConstants.ConstDispType_DISP.Equals(attribute.DISP_type)
                    || CDEConstants.ConstDispType_READONLY.Equals(attribute.DISP_type))
                {
                    DataGridColumn clm = null;
                    Unit width = Unit.Parse(attribute.Dsply_width);

                    if (attribute.getP_Colmun().isHaveCandidate())
                    {
                        clm = new TemplateColumn();
                        ((TemplateColumn)clm).ItemTemplate = new LableCandidateITemplate(
                            attribute.Name, attribute.getP_Colmun().getCandidates());
                    }
                    else if (ConstDataType.BOOLEAN.Equals(attribute.getP_Colmun().Type))
                    {
                        CDECandidate falseCa = new CDECandidate("0:False", "0");
                        CDECandidate trueCa = new CDECandidate("1:True", "1");
                        List<CDECandidate> canlist = new List<CDECandidate>();
                        canlist.Add(falseCa);
                        canlist.Add(trueCa);
                        clm = new TemplateColumn();
                        ((TemplateColumn)clm).ItemTemplate = new LableCandidateITemplate(
                            attribute.Name, canlist);
                    }
                    //else if (ConstDataType.IMAGE.Equals(attribute.getP_Colmun().Type))
                    //{
                    //    clm = new TemplateColumn();
                    //    ((TemplateColumn)clm).ItemTemplate = new ImageResultTemplate(attribute.Name);
                    //}
                    //else if (ConstDataType.FILE.Equals(attribute.getP_Colmun().Type))
                    //{
                    //    clm = new TemplateColumn();
                    //    ((TemplateColumn)clm).ItemTemplate = new FileResultITemplate(attribute.Name);
                    //}
                    else
                    {
                        clm = new TemplateColumn();
                        if (ConstDataType.DATE.Equals(attribute.getP_Colmun().Type))
                        {
                            ((TemplateColumn)clm).ItemTemplate = new CssLableITemplate(attribute.Name);
                        }
                        else
                        {
                            ((TemplateColumn)clm).ItemTemplate = new CssLableITemplate(attribute.Name, attribute.Disp_format); 
                        }
                    }
                    clm.HeaderText = attribute.Label;

                    if (attribute.getP_Colmun().isHaveCandidate())
                    {
                        clm.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                    }
                    else if (ConstDataType.STRING.Equals(attribute.getP_Colmun().Type))
                    {
                        clm.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
                    }
                    else if (ConstDataType.NUMBER.Equals(attribute.getP_Colmun().Type))
                    {
                        clm.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                    }

                    clm.ItemStyle.Width = width;
                    //clm.HeaderStyle.Width = width;
                    sum_width += (int)width.Value;

                    this.Columns.AddAt(clmIndex, clm);
                    clmIndex++;
                }

            }
            sum_width += elseWidth;
            divWidth = new Unit(sum_width);

            foreach (DataGridColumn clm in this.Columns)
            {
                int w = (int)clm.ItemStyle.Width.Value;
                clm.ItemStyle.Width = Unit.Parse(((w * 100) / sum_width) + "%");
            }
        }

        public override bool ShowHeader
        {

            get
            {
                return true;
            }

            set
            {
                if (false == value)
                    throw new InvalidOperationException("Use the original DataGrid to set ShowHeaders to false.");
            }
        }

        public override Unit Width
        {

            get
            {
                return divWidth;
            }
        }

        public override Unit Height
        {

            get
            {
                return divHeight;
            }
        }
    }
}
